package com.nike.controller;

import java.util.List;

import com.nike.model.Productcategory;
import com.nike.router.RouterMapping;
import com.nike.utils.StringUtils;

/**
 * 分类列表
 * 
 * @author wuhantao
 *
 */
@RouterMapping(url = "/productCategory", viewPath = "/view/productCategory")
public class ProductcategoryController extends BaseController<Productcategory> {

	// 列表
	public void index() {

		setAttr("productCategoryTreeList", Productcategory.DAO.getAll());
		render("list.html");
	}
	public void toAdd() {
		setAttr("productCategoryTreeList", Productcategory.DAO.getAll());
		render("add.html");
	}
	// 保存
	public void insert() {
		Productcategory productcategory = getModel(Productcategory.class, "productCategory");
		if (StringUtils.isNotEmpty(productcategory.getParentId())) {
			Productcategory parent = Productcategory.DAO.findById(productcategory.getParentId());
			if (parent != null) {
				String parentPath = parent.getStr("path");
				productcategory.set("path", parentPath + "," + parent.getId());
			} else {
				productcategory.set("path", productcategory.getParentId());
			}
		}
		saved(productcategory);
		redirect("/productCategory");
	}

	// 删除
	public void delete() {
		String id = getPara("id");
		if (StringUtils.isNotBlank(id)) {
			Productcategory productcategory = Productcategory.DAO.findById(id);
			// 是否存在下级
			List<Productcategory> childrenProductCategoryList = productcategory.getChildren();
			if (childrenProductCategoryList != null && childrenProductCategoryList.size() > 0) {
				setAttr("message", "此商品分类存在下级分类，删除失败!");
				return;
			}
			// 是否存在商品
			/*
			 * List<Product> productList = productcategory.getProductList(); if
			 * (productList != null && productList.size() > 0) {
			 * ajaxJsonErrorMessage("此商品分类下存在商品，删除失败!"); return; }
			 */
		}

		Productcategory.DAO.deleteById(id);
		redirect("/productCategory");
	}

	// 编辑
	public void toEdit() {
		String id = getPara("id");
		if (StringUtils.isNotBlank(id)) {
			setAttr("productCategory", Productcategory.DAO.findById(id));
		}
		setAttr("productCategoryTreeList", Productcategory.DAO.getAll());
		render("add.html");
	}
	// 修改
	public void update() {
		Productcategory productcategory = getModel(Productcategory.class, "productCategory");
		updated(productcategory);
		redirect("/productCategory");
	}

}
